package com.example.selenium.util;

import com.alibaba.fastjson.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtilPlus {

    /**
     * 获取今天
     * @return String
     * */
    public static String getToday(){
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    }
    /**
     * 获取昨天
     * @return String
     * */
    public static String getYestoday(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }
    /**
     * 获取本月开始日期
     * @return String
     * **/
    public static String getMonthStart(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 00:00:00";
    }
    /**
     * 获取本月开始日期
     * @return String
     * **/
    public static String getMonthStart(String formater){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date time=cal.getTime();
        return new SimpleDateFormat(formater).format(time);
    }
    /**
     * 获取本月最后一天
     * @return String
     * **/
    public static String getMonthEnd(){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 23:59:59";
    }
    
    /**
     * 获取本月最后一天
     * @return String
     * **/
    public static String getMonthEnd(String formater){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date time=cal.getTime();
        return new SimpleDateFormat(formater).format(time);
    }

    
    
    
    public static String getLastMonthStart(String formater){
        SimpleDateFormat format=new SimpleDateFormat(formater);

        Calendar calendar=Calendar.getInstance();

        calendar.add(Calendar.MONTH, -1);

        calendar.set(Calendar.DAY_OF_MONTH, 1);

        return format.format(calendar.getTime());
     
        }
        
    
    
    
    public static String getLastMonthEnd(String formater){
    	SimpleDateFormat sf=new SimpleDateFormat(formater);

    	Calendar calendar=Calendar.getInstance();

    	int month=calendar.get(Calendar.MONTH);

    	calendar.set(Calendar.MONTH, month-1);

    	calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

    	return sf.format(calendar.getTime());
    	 
 
    }
    
    /**
     * 获取本周的第一天
     * @return String
     * **/
    public static String getWeekStart(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.WEEK_OF_MONTH, 0);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 00:00:00";
    }
    
    /**
     * 获取本周的第一天
     * @return String
     * **/
    public static String getWeekStart(String formater){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.WEEK_OF_MONTH, 0);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        Date time=cal.getTime();
        return new SimpleDateFormat(formater).format(time);
    }
    
    /**
     * 获取本周的最后一天
     * @return String
     * **/
    public static String getWeekEnd(){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
        cal.add(Calendar.DAY_OF_WEEK, 1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 23:59:59";
    }
    /**
     * 获取本年的第一天
     * @return String
     * **/
    public static String getYearStart(){
        return new SimpleDateFormat("yyyy").format(new Date())+"-01-01";
    }
  
    /**
     * 获取本年的最后一天
     * @return String
     * **/
    public static String getYearEnd(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH,calendar.getActualMaximum(Calendar.MONTH));
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date currYearLast = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(currYearLast)+" 23:59:59";
    }
    
    
    
    /**
	 * 获取周一和周日的结果集
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static List<Map<String, String>> getBetweenWeeks(String startDate, String endDate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date start = sdf.parse(startDate);// 定义起始日期
		Date end = sdf.parse(endDate);// 定义结束日期
		// List<Date> result = new ArrayList<Date>();
		List<Map<String, String>> result = new ArrayList<Map<String, String>>();
		Calendar tempStart = Calendar.getInstance();
		tempStart.setTime(start);

		Calendar tempEnd = Calendar.getInstance();
		tempEnd.setTime(end);
		while (tempStart.before(tempEnd) || tempStart.equals(tempEnd)) {
			Map<String, String> map = new HashMap<String, String>();
			int we = tempStart.get(Calendar.DAY_OF_WEEK);
			if (we == 2) {
				map.put("mon", sdf.format(tempStart.getTime()));
			}

			if (map.isEmpty()) { // 检测map是否为空
				tempStart.add(Calendar.DAY_OF_YEAR, 1);
			} else {
				tempStart.add(Calendar.DAY_OF_YEAR, 6);
				map.put("sun", sdf.format(tempStart.getTime()));
				result.add(map);
			}
		}
		return result;
	}
	
	
	/**
	 * 获取周一和周日的结果集
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static List<Map<String, String>> getBetweenWeeksEx(String startDate, String endDate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date start = sdf.parse(startDate);// 定义起始日期
		Date end = sdf.parse(endDate);// 定义结束日期
		// List<Date> result = new ArrayList<Date>();
		List<Map<String, String>> result = new ArrayList<Map<String, String>>();
		Calendar tempStart = Calendar.getInstance();
		tempStart.setTime(start);

		Calendar tempEnd = Calendar.getInstance();
		tempEnd.setTime(end);
		while (tempStart.before(tempEnd) || tempStart.equals(tempEnd)) {
			Map<String, String> map = new HashMap<String, String>();
			int we = tempStart.get(Calendar.DAY_OF_WEEK);
			if (we == 2) {
				map.put("mon", sdf.format(tempStart.getTime())+" 00:00:00");
			}

			if (map.isEmpty()) { // 检测map是否为空
				tempStart.add(Calendar.DAY_OF_YEAR, 1);
			} else {
				tempStart.add(Calendar.DAY_OF_YEAR, 6);
				map.put("sun", sdf.format(tempStart.getTime())+" 23:59:59");
				result.add(map);
			}
		}
		return result;
	}
	
	public static void main(String[] args) {
		try {
		System.out.println(JSONObject.toJSONString(getBetweenWeeksEx("2019-12-30", "2020-04-12")));	
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
